package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ProductCategory;
import cn.wolfcode.crm.service.IProductCategoryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 丧彪
 */
@Controller
@RequestMapping("productCategory")
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService productCategoryService;
    
    @RequiresPermissions("productCategory:index")
    @PermissionName("商品首页")
    @RequestMapping("")
    public String index(){
        return "productCategory/list";
    }

    @RequiresPermissions("productCategory:list")
    @PermissionName("商品列表")
    @RequestMapping("list")
    @ResponseBody
    public List<ProductCategory> list(){
        return productCategoryService.getAll();
    }
    
    @RequiresPermissions("productCategory:save")
    @PermissionName("商品保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(ProductCategory entity){
    	JsonResult result = new JsonResult();
    	try {
    		productCategoryService.save(entity);
    		result.markMsg("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("保存失败" + e.getMessage());
		}
        return result;
    }
    
    @RequiresPermissions("productCategory:update")
    @PermissionName("商品更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(ProductCategory entity){
    	JsonResult result = new JsonResult();
        result.markMsg("编辑成功");
    	try {
    		productCategoryService.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("更新失败" + e.getMessage());
		}
        return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            if (id!=null) {
                productCategoryService.delete(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("请先删除一级分类");
        }
        return result;
    }
    @RequestMapping("query")
    @ResponseBody
    public List<ProductCategory> query(Long parentId){
        List<ProductCategory> productCategories = productCategoryService.query(parentId);

        return productCategories;
    }


}
